package com.getpebble.android.common.model;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.PblDataModel.Column;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.io.PrintStream;

public class PreferenceModel extends PblDataModel {
    public static final Uri TABLE_URI = PblContentProviderUtil.getTableUri("preferences");
    private static final String TAG = PreferenceModel.class.getSimpleName();

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type = new int[Type.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.STRING.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.STRING_SET.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.INT.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.LONG.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.FLOAT.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.BOOLEAN.ordinal()] = 6;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[Type.UNKNOWN.ordinal()] = 7;
            } catch (NoSuchFieldError e7) {
            }
        }
    }

    public static class Preference {
        public final String key;
        public final Type type;
        public final String value;

        public Preference(String key, Type type, String value) {
            this.key = key;
            this.type = type;
            this.value = value;
        }

        public String toString() {
            return "Preference{key='" + this.key + '\'' + ", type=" + this.type + ", value='" + this.value + '\'' + '}';
        }
    }

    public enum Type {
        UNKNOWN(0),
        STRING(1),
        INT(2),
        LONG(3),
        FLOAT(4),
        BOOLEAN(5),
        STRING_SET(6);
        
        private final int mValue;

        private Type(int value) {
            this.mValue = value;
        }

        public int getValue() {
            return this.mValue;
        }

        public static Type from(int value) {
            for (Type type : values()) {
                if (type.getValue() == value) {
                    return type;
                }
            }
            return UNKNOWN;
        }
    }

    public PreferenceModel() throws IllegalArgumentException {
        super("preferences");
        addColumn(new Column(com.getpebble.android.common.model.PblDataModel.Column.Type.STRING, "name"));
        addColumn(new Column(com.getpebble.android.common.model.PblDataModel.Column.Type.STRING, "key"));
        addColumn(new Column(com.getpebble.android.common.model.PblDataModel.Column.Type.INTEGER, "type"));
        addColumn(new Column(com.getpebble.android.common.model.PblDataModel.Column.Type.STRING, "value"));
    }

    public String getCreateTableCommand() {
        StringBuilder createCommand = new StringBuilder(super.getCreateTableCommand());
        String constraint = ", UNIQUE(" + "name" + ", " + "key" + ")";
        createCommand.insert(createCommand.length() - 2, constraint, 0, constraint.length());
        return createCommand.toString();
    }

    public static boolean put(ContentResolver contentResolver, String name, Preference preference) {
        ContentValues contentValues = new ContentValues(3);
        contentValues.put("name", name);
        contentValues.put("key", preference.key);
        contentValues.put("type", Integer.valueOf(preference.type.getValue()));
        contentValues.put("value", preference.value);
        if (contains(contentResolver, name, preference.key)) {
            String selection = "key = ? AND name = ?";
            try {
                contentResolver.update(TABLE_URI, contentValues, "key = ? AND name = ?", new String[]{preference.key, name});
                return true;
            } catch (Exception e) {
                Trace.verbose(TAG, "Failed to update data", e);
                return false;
            }
        }
        try {
            contentResolver.insert(TABLE_URI, contentValues);
            return true;
        } catch (Exception e2) {
            Trace.verbose(TAG, "Failed to insert data; updating", e2);
            return false;
        }
    }

    public static boolean contains(ContentResolver contentResolver, String name, String key) {
        String selection = "key = ? AND name = ?";
        ContentResolver contentResolver2 = contentResolver;
        Cursor cursor = contentResolver2.query(TABLE_URI, new String[]{"value"}, "key = ? AND name = ?", new String[]{key, name}, null);
        if (cursor == null) {
            return false;
        }
        boolean isStored = cursor.moveToFirst();
        cursor.close();
        return isStored;
    }

    public static ContentValues cursorRowToContentValue(Cursor cursor) {
        ContentValues result = new ContentValues(1);
        Type type = Type.from(cursor.getInt(cursor.getColumnIndex("type")));
        String key = cursor.getString(cursor.getColumnIndex("key"));
        String value = cursor.getString(cursor.getColumnIndex("value"));
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$common$model$PreferenceModel$Type[type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
            case SMTPMessage.RETURN_HDRS /*2*/:
                result.put(key, value);
                break;
            case ListInfo.INDETERMINATE /*3*/:
                try {
                    result.put(key, Integer.valueOf(value));
                    break;
                } catch (NumberFormatException e) {
                    Trace.debug(TAG, "NumberFormatException", e);
                    break;
                }
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                try {
                    result.put(key, Long.valueOf(value));
                    break;
                } catch (NumberFormatException e2) {
                    Trace.debug(TAG, "NumberFormatException", e2);
                    break;
                }
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsHeaderTextAppearance /*5*/:
                try {
                    result.put(key, Float.valueOf(value));
                    break;
                } catch (NumberFormatException e22) {
                    Trace.debug(TAG, "NumberFormatException", e22);
                    break;
                }
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsBackground /*6*/:
                try {
                    result.put(key, Boolean.valueOf(value));
                    break;
                } catch (NumberFormatException e222) {
                    Trace.debug(TAG, "NumberFormatException", e222);
                    break;
                }
            default:
                Trace.debug(TAG, "Unknown key: " + key);
                break;
        }
        return result;
    }

    public static boolean remove(ContentResolver contentResolver, String name, String key) {
        String selection = "name = ? AND key = ?";
        if (contentResolver.delete(TABLE_URI, "name = ? AND key = ?", new String[]{name, key}) > 0) {
            return true;
        }
        return false;
    }

    public static void removeAll(ContentResolver contentResolver, String name) {
        String selection = "name = ?";
        int rows = contentResolver.delete(TABLE_URI, "name = ?", new String[]{name});
        Trace.verbose(TAG, String.format("Removed %d rows from file %s", new Object[]{Integer.valueOf(rows), name}));
    }

    public static void catPreferencesToStream(ContentResolver contentResolver, PrintStream printStream) {
        Cursor cursor = contentResolver.query(TABLE_URI, null, null, null, "name ASC");
        DatabaseUtils.dumpCursor(cursor, printStream);
        if (cursor != null) {
            cursor.close();
        }
    }
}
